//
//  AiRoleBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/8.
//

import Foundation
import LeanCloud
import GRDB

//图片
class CloudPictureBean : ICloudTable,IDbTable, Identifiable {
    func getCloudTable() -> String {
        return "CloudPicture"
    }
    
    static let databaseTableName = "picture"

    
    //leanCloud 排除字段
    func doCustomExcluded() -> [String? : Any?] {
        return doExcluded(subject: self, excluded: ["createTime","updateTime","modifySign","status", "urlTime"])
    }
    
    //db排除字段
    func doCustomDBExcluded()  -> [String? : Any?] {
        return doExcluded(subject: self, excluded: [""])
    }
    
    var objectId: String?
    var diaryId: String? = nil
    var fileId: String? = nil //文件id，第三方上传的唯一表示
    var folderId:String? = nil //目录id
    var userId: String? = nil
    var thumbnail: String? = nil //缩略图
    var name:String? = nil
    var letterNum:String? = nil
    var url:String? = nil //真实地址
    var duration:Int64 = 0 //时长
    var width : Int? = nil //宽
    var height : Int? = nil //宽
    var time:Int64? = nil//时间
    var sort:Int? = nil//排序
    var size:Int64? = nil//大小
    var hash:String? = nil
    var mimeType:String? = nil
    var describe: String? = nil//图片理解
    var oldPath: String? = nil//旧路径
    
    var createTime: Int64 = 0//创建时间
    var updateTime: Int64 = 0//更新时间时间
    var modifySign: Bool = false//修改标识
    var status: Int = IStatusType.NORMAL.rawValue
    var urlTime: Int64 = 0 //url过期时间

    
    func isVideo() -> Bool {
        return duration > 0
    }
    func isImage() -> Bool {
        //时长==0
        return duration == 0
    }
    
    /**
     * 用于ai图片要识别的路径
     */
    func getAiPath()  -> String? {
        if oldPath?.isEmpty == false {
            return oldPath
        }
        return thumbnail
    }
    
    //json解析白名单
    private enum CodingKeys: String, CodingKey {
        case objectId
        case diaryId
        case fileId
        case folderId
        case userId
        case thumbnail
        case name
        case letterNum
        case url
        case duration
        case width
        case height
        case time
        case sort
        case size
        case hash
        case mimeType
        case describe
        case oldPath
        //        case createTime
        //        case updateTime
        //        case modifySign
        //        case status
        //        case urlTime
    }
}

extension CloudPictureBean {
    // db数据库指定字段
    public func encode(to container: inout PersistenceContainer) {
        doDBExcluded(to: &container)
    }
}
